using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = System.Random;

public class LDPlayerManager : MonoBehaviour
{
    private static LDPlayerManager _ldPlayerManager;

    public static LDPlayerManager getInstance()
    {
        return _ldPlayerManager;
    }

    public List<MoveMsg> serverMsgs = new List<MoveMsg>();
    public List<MoveMsg> remoteMsgs = new List<MoveMsg>();
    public int GlobalDelay = 60;//全局延迟50ms
    public int rangeDelay = 40;//全局延迟50ms
    public LocalDemoPlayer localPlayer;
    public LocalDemoPlayer serverPlayer;
    public LocalDemoPlayer remotePlayer;

    private void Awake()
    {
        _ldPlayerManager = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        long nowTicks = getTimestamps();
        //先处理消息
        if (serverMsgs.Count > 0)
        {
            MoveMsg serverMsg = serverMsgs[0];
            if (nowTicks>=serverMsg.receiveTime)
            {
                serverMsgs.RemoveAt(0);
                //处理消息
                // serverMsg.
                float duration = (nowTicks-serverMsg.sendTime)/1000f;//发送到接收处理间隔的时间(模拟网络传输时间)
                Vector3 move = new Vector3(serverMsg.velocity.x,serverMsg.velocity.y,0)*serverPlayer.speed*duration;
                Vector3 serverStartMovePos = new Vector3(serverMsg.position.x,serverMsg.position.y) + move;
                serverPlayer.obj.transform.position = serverStartMovePos;
                serverPlayer.dir = serverMsg.velocity;

                //TODO 回发给客户端，让客户端校验
                //转发给其他客户端
                //
                serverMsg.position = serverStartMovePos;
                sendRemoteMsg(serverMsg);
            }
        }
        if (remoteMsgs.Count > 0)
        {
            MoveMsg remoteMsg = remoteMsgs[0];
            if (nowTicks>=remoteMsg.receiveTime)
            {
                remoteMsgs.RemoveAt(0);
                //处理消息
                // serverMsg.
                float duration = (nowTicks-remoteMsg.sendTime)/1000f;//发送到接收处理间隔的时间(模拟网络传输时间)
                Vector3 move = new Vector3(remoteMsg.velocity.x,remoteMsg.velocity.y,0)*remotePlayer.speed*duration;
                Vector3 remotetartMovePos = new Vector3(remoteMsg.position.x,remoteMsg.position.y) + move;
                remotePlayer.obj.transform.position = remotetartMovePos;
                remotePlayer.dir = remoteMsg.velocity;

                //TODO 回发给客户端，让客户端校验
                //转发给其他客户端
                //
            }
        }
        //所有角色移动
        //localPlayer
        playerMove(localPlayer);
        playerMove(serverPlayer);
        playerMove(remotePlayer);

    }

    private void playerMove(LocalDemoPlayer player)
    {
        if(player.dir!=Vector2.zero)
        {
            GameObject playerObj = player.obj;
            if (playerObj != null)
            {
                Vector3 dir = new Vector3(player.dir.x, player.dir.y, 0);
                player.transform.Translate(dir*player.speed*Time.deltaTime,Space.World);
            }
        }
    }

    public void sendServerMsg(MoveMsg msg)
    {
        msg.sendTime = getTimestamps();
        Random random = new Random();
        int receiveTime = random.Next(GlobalDelay - rangeDelay, GlobalDelay + rangeDelay);
        msg.receiveTime = msg.sendTime+receiveTime;

        serverMsgs.Add(msg);
    }
    public void sendRemoteMsg(MoveMsg msg)
    {
        msg.sendTime = getTimestamps();
        Random random = new Random();
        int receiveTime = random.Next(GlobalDelay - rangeDelay, GlobalDelay + rangeDelay);
        msg.receiveTime = msg.sendTime+receiveTime;

        remoteMsgs.Add(msg);
    }

    public long getTimestamps()
    {
        long nowTicks = DateTime.Now.Ticks;
        DateTime dfFrom = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return (nowTicks - dfFrom.Ticks) / 10000;
    }
}
